package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.ProductlogMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Productlog;
import com.qingcheng.service.goods.ProductlogService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class ProductlogServiceImpl implements ProductlogService {

    @Autowired
    private ProductlogMapper productlogMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<Productlog> findAll() {
        return productlogMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Productlog> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Productlog> productlogs = (Page<Productlog>) productlogMapper.selectAll();
        return new PageResult<Productlog>(productlogs.getTotal(),productlogs.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Productlog> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return productlogMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Productlog> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Productlog> productlogs = (Page<Productlog>) productlogMapper.selectByExample(example);
        return new PageResult<Productlog>(productlogs.getTotal(),productlogs.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Productlog findById(String id) {
        return productlogMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param productlog
     */
    public void add(Productlog productlog) {
        productlogMapper.insert(productlog);
    }

    /**
     * 修改
     * @param productlog
     */
    public void update(Productlog productlog) {
        productlogMapper.updateByPrimaryKeySelective(productlog);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(String id) {
        productlogMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Productlog.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // id
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
            }
            // spu商品的id
            if(searchMap.get("spuId")!=null && !"".equals(searchMap.get("spuId"))){
                criteria.andLike("spuId","%"+searchMap.get("spuId")+"%");
            }
            // 原上架状态
            if(searchMap.get("isMarketableBef")!=null && !"".equals(searchMap.get("isMarketableBef"))){
                criteria.andLike("isMarketableBef","%"+searchMap.get("isMarketableBef")+"%");
            }
            // 现上架状态
            if(searchMap.get("isMarketableNow")!=null && !"".equals(searchMap.get("isMarketableNow"))){
                criteria.andLike("isMarketableNow","%"+searchMap.get("isMarketableNow")+"%");
            }
            // 原审核状态
            if(searchMap.get("statusBef")!=null && !"".equals(searchMap.get("statusBef"))){
                criteria.andLike("statusBef","%"+searchMap.get("statusBef")+"%");
            }
            // 现审核状态
            if(searchMap.get("statusNow")!=null && !"".equals(searchMap.get("statusNow"))){
                criteria.andLike("statusNow","%"+searchMap.get("statusNow")+"%");
            }
            // 审核人员
            if(searchMap.get("auditor")!=null && !"".equals(searchMap.get("auditor"))){
                criteria.andLike("auditor","%"+searchMap.get("auditor")+"%");
            }

            // 原价格
            if(searchMap.get("priceBef")!=null ){
                criteria.andEqualTo("priceBef",searchMap.get("priceBef"));
            }
            // 现价格
            if(searchMap.get("priceNow")!=null ){
                criteria.andEqualTo("priceNow",searchMap.get("priceNow"));
            }
            // 促销前
            if(searchMap.get("onSaleBef")!=null ){
                criteria.andEqualTo("onSaleBef",searchMap.get("onSaleBef"));
            }
            // 促销后
            if(searchMap.get("onSaleNow")!=null ){
                criteria.andEqualTo("onSaleNow",searchMap.get("onSaleNow"));
            }
            // 赠送匠币前
            if(searchMap.get("presBef")!=null ){
                criteria.andEqualTo("presBef",searchMap.get("presBef"));
            }
            // 赠送匠币后
            if(searchMap.get("presNow")!=null ){
                criteria.andEqualTo("presNow",searchMap.get("presNow"));
            }
            // 匠币购买金额前
            if(searchMap.get("buyBef")!=null ){
                criteria.andEqualTo("buyBef",searchMap.get("buyBef"));
            }
            // 匠币购买金额后
            if(searchMap.get("buyNow")!=null ){
                criteria.andEqualTo("buyNow",searchMap.get("buyNow"));
            }

        }
        return example;
    }

}
